﻿using UnityEngine;

public class DestroyByContact : MonoBehaviour {
	// === Unity ======================================================================================================
	public GameObject explosion;
	public GameObject playerExplosion;
	public int scoreValue;
	
	private void Start() {
		var gameControllerObject = GameObject.FindWithTag("GameController");
		if (gameControllerObject != null) {
			gameController = gameControllerObject.GetComponent<GameController>();
		}
		if (gameController == null) {
			Debug.Log("Cannot find 'GameController' script");
		}
	}

	private void OnTriggerEnter(Collider other) {
		if (other.tag == "Boundary") {
			return;
		}
		Instantiate(explosion, transform.position, transform.rotation);
		if (other.tag == "Player") {
			Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
			gameController.GameOver();
		}
		gameController.AddScore(scoreValue);
		Destroy(other.gameObject);
		Destroy(gameObject);
	}

	// === Private ====================================================================================================
	private GameController gameController;
}
